package task.snake.model;

import java.awt.Point;

import task.snake.model.Snake.Eat;

/**
 * Class Frog extends Creature class and describes frog entity. Class provides
 * constructor
 * 
 * @author Nadejda Makarkina
 * 
 */
public class Frog extends Creature {
	final String TYPE_OF_FROG = "Green";
	
	/**
	 * Initializes a newly created Frog object.
	 * 
	 * @param p position on play field
	 * @param sm snake model object
	 */
	public Frog(Point p, SnakeModel sm) {
		super(p, sm, sm.getSleepTime() * 3);
	}

	/**
	 * Method returns type of frog.
	 */
	public String getTypeOfFrog() {
		return TYPE_OF_FROG;
	}

	/**
	 * Method generates random number from 0 to 3 to move frog 0 - up, 1 -
	 * right, 2 - down, 3 - left and then move frog to new location.
	 */
	@Override
	public void move() {
		if (onClash(sm.getSnake())) {
			return;
		}
		boolean continueLoop = true;
		// if all next cells are busy frog dosn't move, else move
		if (!sm.getCell(body.get(0)).isAllDirectionsBusy()) {
			Point oldPosition = new Point (body.get(0));
			do {
				int rand = (int) (Math.random() * 4);
				switch (rand) {
				case 0:
					if (!sm.getCell(getHeadLocation()).getUp()) {
						getHeadLocation().y = body.get(0).y - 1;
						continueLoop = false;
					}
					break;
				case 1:
					if (!sm.getCell(getHeadLocation()).getRight()) {
						getHeadLocation().x = body.get(0).x + 1;
						continueLoop = false;
					}
					break;
				case 2:
					if (!sm.getCell(getHeadLocation()).getDown()) {
						getHeadLocation().y = body.get(0).y + 1;
						continueLoop = false;
					}
					break;
				case 3:
					if (!sm.getCell(getHeadLocation()).getLeft()) {
						getHeadLocation().x = body.get(0).x - 1;
						continueLoop = false;
					}
					break;
				}
			} while (continueLoop);
			// after movement setup old sell free
			sm.setCell(oldPosition, null, false);
			// and new cell busy
			sm.setCell(getHeadLocation(), this, true);
		}
	}

	/**
	 * Describe situation when frog clashed by the snake.
	 */
	@Override
	public boolean onClash(Snake snake) {
		if (snake.getHeadLocation().equals(this.getHeadLocation())) {
			sm.setCounter(sm.getCounter() + 1);
			// tells snake that it has to grow up while net movement
			snake.setEat(Eat.GREEN_FROG);
			// kills frog and create new one
			for (int i = 0; i < sm.getFrogs().size(); i++) {
				// looks for the frog to kill
				if (sm.getFrogs().get(i) == this) {
					// stops the thread
					sm.getFrogs().get(i).setStopped();
					// sets cell free
					sm.setCell(sm.getFrogs().get(i).getHeadLocation(), null,
							false);
					// kills old frog
					sm.getFrogs().remove(i);
					// creates new frog
					sm.getFrogs().add(i, sm.createFrog());
					// sets cell busy
					sm.setCell(sm.getFrogs().get(i).getHeadLocation(), sm
							.getFrogs().get(i), true);
					// creates new thread for it					
					Thread t = new Thread(sm.getFrogs().get(i));
					t.setName("Frog " + i);
					// and starts it
					t.start();
					break;
				}
			}
			return true;
		}
		return false;
	}
}
